< previous page page_542 next page >

Page 542
The Solid3d's SortFaces subroutine rearranges the solid's faces so that those furthest from the center of projection come first. The solid can then draw its faces in farthest-to-nearest order to cover any surfaces that should be hidden.
Program Depth1 uses a very simple method for determining the order in which the solids should be drawn. It assumes that the object with the smallest Z coordinate after transformation is the one furthest from the center of projection.
This works well for convex solids that are relatively far apart. When the objects are close together, however, this method sometimes fails. Unfortunately, the surfaces that make up a nonconvex solid are guaranteed to be close together. In that case, this simple method does not always work.
Figure 15.8 shows a solid after its points have been transformed for projection. Surface A has a smaller Z coordinate than surface B, even though it obscures surface B. The Depth-Sort algorithm as written above would draw surface A first, even though it belonged in front of surface B and therefore should have been drawn second. If you use program Depth1 to view the stellate octahedron, you can find some viewing directions from which this happens, and surfaces that should be hidden are visible.
To make the algorithm handle this common situation, the SortFaces routine used by the Solid3d class must be a bit more sophisticated. This routine begins by moving any backfaces into the collection that will hold the sorted faces. Because the backfaces are never drawn, it does not matter where they are in the rearranged list. By removing them right away, SortFaces reduces the number of objects it must consider so that it can run more quickly.
z0562-01.gif
Figure 15.8
A simple Depth-Sort algorithm does not work for this solid.

 
< previous page page_542 next page >